﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.IO;

namespace PJAA_Inventory_and_AR___Prototype
{
    public partial class View_Accounts_Receivable : Form
    {
        private SQLMaster master;
        List<object[]> dataCompare = new List<object[]>();

        public View_Accounts_Receivable()
        {
            //added this block to make the form transparent
            SetStyle(ControlStyles.SupportsTransparentBackColor, true);
            this.BackColor = Color.Transparent;

            InitializeComponent();
            master = new SQLMaster();
            dataCompare = master.loadAR();
            loadAR();
        }


        private void loadAR()
        {
            ViewAR_DGV.Rows.Clear();
            foreach (object[] strArray in dataCompare)
            {
                if (ViewAR_Name_TxtBox.Text != string.Empty)
                {
                    if ((strArray[2] as string).ToLower().Contains(ViewAR_Name_TxtBox.Text))
                    {
                        ViewAR_DGV.Rows.Add(strArray);
                    }
                }
                else
                    ViewAR_DGV.Rows.Add(strArray);
            }
        }

        private void ViewAR_Close_Btn_Click(object sender, EventArgs e)
        {
            Close();
        }

        private void ViewAR_Name_TxtBox_TextChanged(object sender, EventArgs e)
        {
            loadAR();
        }

        private void Print_AR_btn_Click(object sender, EventArgs e)
        {
            {
                if (saveFileDialog1.ShowDialog() == DialogResult.OK)
                {
                    FileStream fs = new FileStream(saveFileDialog1.FileName, FileMode.Create);
                    string strExport = "";
                    //Loop through all the columns in DataGridView to Set the 
                    //Column Heading
                    foreach (DataGridViewColumn dc in ViewAR_DGV.Columns)
                    {
                        strExport += dc.HeaderText + ",";
                    }
                    strExport = strExport.Substring(0, strExport.Length - 3) + Environment.NewLine.ToString();
                    //Loop through all the row and append the value with 3 spaces
                    foreach (DataGridViewRow dr in ViewAR_DGV.Rows)
                    {
                        foreach (DataGridViewCell dc in dr.Cells)
                        {
                            if (dc.Value != null)
                            {
                                strExport += dc.Value.ToString() + ",";
                            }
                        }
                        strExport += Environment.NewLine.ToString();
                    }
                    strExport = strExport.Substring(0, strExport.Length - 3) + Environment.NewLine.ToString();
                    //Create a TextWrite object to write to file, select a file name with .csv extention
                    System.IO.TextWriter tw = new System.IO.StreamWriter(fs);
                    //Write the Text to file
                    tw.Write(strExport);
                    //Close the Textwrite
                    tw.Close();

                }
            }
        }
    }
}
